package com.itheima.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        ServletContext servletContext = req.getServletContext();
        String username = (String)req.getParameter("username");
        String password = (String)req.getParameter("password");
        System.out.println(username+password);
        if ("1".equals(username)&&"1".equals(password)) {
            //登录成功 跳转到主页 (用重定向还是转发？？？？？)
            resp.sendRedirect(req.getContextPath()+"/opera/home.jsp");
            //req.getRequestDispatcher( "/opera/home.jsp").forward(req, resp);
            servletContext.setAttribute("lock","lock");
        }else{
            resp.getWriter().write("fail");
           resp.setHeader("Refresh","5;URL="+req.getContextPath()+"/login/login.jsp");
            //增加点击跳转方式
            resp.getWriter().write("<a href="+req.getContextPath()+"/login/login.jsp>如果未跳转到登录页面，可点击此链接进行跳转</a>");

        }
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
